
package com.leaves.sys.modular.auth.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leaves.core.consts.CommonConstant;
import com.leaves.core.context.constant.ConstantContextHolder;
import com.leaves.core.email.MailSender;
import com.leaves.core.email.modular.model.SendMailParam;
import com.leaves.core.enums.CommonStatusEnum;
import com.leaves.core.exception.AuthException;
import com.leaves.core.exception.ServiceException;
import com.leaves.core.exception.enums.RegisterExceptionEnum;
import com.leaves.core.pojo.base.param.BaseParam;
import com.leaves.core.pojo.response.ErrorResponseData;
import com.leaves.core.pojo.response.ResponseData;
import com.leaves.core.pojo.response.SuccessResponseData;

import com.leaves.sys.modular.auth.param.RegisterUserParams;
import com.leaves.sys.modular.auth.service.AuthService;
import com.leaves.sys.modular.user.entity.SysUser;
import com.leaves.sys.modular.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 控制器
 *
 * @author xuyuxiang
 * @date 2020/3/11 12:20
 */
@RestController
@Api(tags = "用户注册接口")
public class SysRegisterController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private AuthService authService;
    /**
     * 用户注册
     */
    @PostMapping("/register/save")
    @ApiOperation(value = "用户注册")
    public ResponseData save(@RequestBody @Validated(BaseParam.register.class) RegisterUserParams registerUserParams) {
        String account = registerUserParams.getAccount();
/*
        String phone = registerUserParams.getPhone();
*/
        //验证账号是否存在
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(sysUserLambdaQueryWrapper -> {
            sysUserLambdaQueryWrapper.eq(SysUser::getAccount, account);
        });
        queryWrapper.ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode());

        int countByAccount = sysUserService.count(queryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new ServiceException(RegisterExceptionEnum.EMAIL_ACCOUNT_REPEAT);
        }
        //验证通过，进行注册逻辑
        authService.registerSave(registerUserParams);
        return new SuccessResponseData();
    }

}
